const resolve = dir => require('path').join(__dirname, dir)
let darkHue = process.env.VUE_APP_VERSIONS == 'dark'
function addLightStyleResource(rule) {
  rule.use('style-resource')
    .loader('style-resources-loader')
    .options({
      patterns: [
        resolve(`src/components/variate.less`),
      ],
    })
}
function addDarkStyleResource(rule) {
  rule.use('style-resource')
    .loader('style-resources-loader')
    .options({
      patterns: [
        resolve(`src/assets/css/dark-hue.less`),
        resolve(`src/components/variate.less`),
      ],
    })
}
let deploy = {
  publicPath: '/',
  productionSourceMap: false,
  chainWebpack: (config) => {
    config.resolve.alias
      .set('~assets', resolve('src/assets'))
      .set('~components', resolve('src/components'))
      .set('~store', resolve('src/store'))
    const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
    types.forEach(type => {
      if (darkHue) {
        addDarkStyleResource(config.module.rule('less').oneOf(type))
      } else {
        addLightStyleResource(config.module.rule('less').oneOf(type))
      }
    })
  },
  configureWebpack: config => {
    config.module.rules.push({
      // 处理markdown文件
      test: /\.md$/,
      use: [
        { loader: "html-loader" },
        { loader: "markdown-loader" }
      ],
    });
   
    config.optimization.splitChunks.cacheGroups = {
      vendor: {
        chunks: 'all',
        test: /node_modules/,
        name: 'vendor',
        minChunks: 1,
        maxInitialRequests: 5,
        minSize: 0,
        priority: 100
      },
      common: {
        chunks: 'all',
        test: /[\\/]src[\\/]js[\\/]/,
        name: 'common',
        minChunks: 2,
        maxInitialRequests: 5,
        minSize: 0,
        priority: 60
      },
      styles: {
        name: 'styles',
        test: /\.(sa|sc|c)ss$/,
        chunks: 'all',
        enforce: true
      },
      runtimeChunk: {
        name: 'manifest'
      }
    }
    // if (process.env.NODE_ENV === 'production') {
    //   config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
    // }
  },
  devServer: {
    disableHostCheck: true,
    proxy: {
      '/api': {
        target: 'https://api.platform.baai.ac.cn/',
        changeOrigin: true,
        pathRewrite: {
          // "^/kuai": ''
          '^/api': ''
        }
      }
    }
  },
}

deploy.publicPath = '/console'
module.exports = deploy
